<template>
  <div class="front-header">
    <div class="header-bg-svg">
      <svg width="100%" height="48" viewBox="0 0 1440 48" fill="none" xmlns="http://www.w3.org/2000/svg">
        <ellipse cx="720" cy="44" rx="720" ry="12" fill="#e8f5e9"/>
        <path d="M0 44 Q720 0 1440 44" stroke="#a5d6a7" stroke-width="4" fill="none"/>
      </svg>
    </div>
    <div class="header-container">
      <div class="logo-area" @click="$router.push('/')">
        <div class="logo-icon">
          <svg width="28" height="28" viewBox="0 0 28 28" fill="none" xmlns="http://www.w3.org/2000/svg">
            <ellipse cx="14" cy="14" rx="12" ry="7" fill="#e8f5e9"/>
            <path d="M6 20 Q14 4 22 20" stroke="#67c23a" stroke-width="2" fill="none"/>
            <ellipse cx="14" cy="14" rx="2.5" ry="5" fill="#a5d6a7"/>
          </svg>
        </div>
        <span class="logo-text">农产品融销一体平台</span>
      </div>

      <div class="nav-area">
        <el-menu mode="horizontal" :router="true" :default-active="activePath" active-text-color="#67C23A" text-color="#333" class="nav-menu">
          <el-menu-item index="/" class="nav-item">首页</el-menu-item>
          <el-menu-item index="/products" class="nav-item">全部商品</el-menu-item>
          <el-menu-item index="/cart" class="nav-item">购物车</el-menu-item>
          <el-menu-item index="/favorite" class="nav-item">我的收藏</el-menu-item>
          <el-menu-item index="/order" class="nav-item">我的订单</el-menu-item>
          <el-menu-item index="/user-center" class="nav-item">个人中心</el-menu-item>
          <el-menu-item index="/front/knowledge" class="nav-item">农业知识</el-menu-item>
          <el-menu-item index="/experts" class="nav-item">专家指导</el-menu-item>
          <el-menu-item index="/finance-manager" class="nav-item">融资申请</el-menu-item>
        </el-menu>
      </div>

      <div class="header-right">
        <div class="search-area">
          <el-input v-model="searchKey" placeholder="搜索商品..." prefix-icon="el-icon-search" clearable class="search-input" @keyup.enter.native="handleSearch">
          </el-input>
        </div>
        <div class="user-area">
          <div v-if="!isLoggedIn" class="login-btn" @click="goToLogin">
            <i class="el-icon-user"></i>
            <span>登录</span>
          </div>
          <div v-else class="user-info">
            <el-dropdown>
              <div class="user-dropdown">
                <span class="username">{{ userInfo.username }}</span>
                <i class="el-icon-arrow-down"></i>
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="goToUserCenter">个人中心</el-dropdown-item>
                <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <div class="admin-btn" @click="goToAdmin">
            <i class="el-icon-s-tools"></i>
            <span>后台</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import router from '@/router';

export default {
  name: 'FrontHeader',
  data() {
    return {
      searchKey: '',
      isLoggedIn: false,
      userInfo: {},
    
    }
  },
  created() {
    this.checkLoginStatus();

  },
  mounted() {

  },
  methods: {
    checkLoginStatus() {
      const userInfo = localStorage.getItem('frontUser');
      if (userInfo) {
        this.isLoggedIn = true;
        this.userInfo = JSON.parse(userInfo);
      }
    },
    goToLogin() {
      this.$router.push('/login')
    },
    goToUserCenter() {
      this.$router.push('/user-center')
    },
    logout() {
      localStorage.removeItem('token');
      localStorage.removeItem('frontUser');
      this.isLoggedIn = false;
      this.userInfo = {};
      this.$message.success('已退出登录');
      this.$router.push('/');
      window.location.reload()
    },
    handleSearch() {
      if (!this.searchKey.trim()) {
        this.$message({
          type: 'warning',
          message: '请输入搜索关键词'
        })
        return
      }

      this.$router.push({
        path: '/search',
        query: {
          keyword: this.searchKey.trim()
        }
      })

      this.searchKey = ''
    },
    goToAdmin() {
      // 检查是否已登录
      const userMenuList = localStorage.getItem('userMenuList');
      const backUser = localStorage.getItem('backUser');
      if (!userMenuList || !backUser) {
        this.$message.warning('请先登录');
        this.$router.push('/login');
        return;
      }
      
      this.$router.push('/showView');
    }
  },
  computed: {
    activePath() {
      return this.$router.currentRoute.fullPath;
    }
  }
}
</script>

<style scoped>
.front-header {
  position: sticky;
  top: 0;
  z-index: 1000;
  background: linear-gradient(90deg, #e8f5e9 60%, #fffde7 100%);
  box-shadow: 0 2px 16px rgba(103, 194, 58, 0.10);
  min-height: 80px;
}

.header-bg-svg {
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 0;
  pointer-events: none;
}

.header-container {
  max-width: 1600px;
  margin: 0 auto;
  padding: 0 18px;
  height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  z-index: 1;
}

.logo-area {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 0 10px;
  border-radius: 16px;
  transition: all 0.3s ease;
  background: rgba(232,245,233,0.5);
  box-shadow: 0 2px 8px #a5d6a722;
}

.logo-area:hover {
  background: rgba(103, 194, 58, 0.13);
}

.logo-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 14px;
  background: #e8f5e9;
  margin-right: 12px;
  box-shadow: 0 2px 8px #a5d6a733;
}

.logo-text {
  font-size: 22px;
  font-weight: 700;
  color: #388e3c;
  letter-spacing: 1.2px;
  font-family: 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Arial', sans-serif;
  text-shadow: 0 2px 8px #388e3c22;
}

.nav-area {
  flex: 1;
  margin: 0 24px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.nav-menu {
  border-bottom: none;
  background: transparent;
  min-width: 800px;
  display: flex;
  justify-content: center;
}

.nav-item {
  font-size: 16px;
  font-weight: 600;
  padding: 0 18px;
  height: 60px;
  line-height: 60px;
  border-radius: 12px 12px 0 0;
  transition: all 0.3s ease;
}

.nav-item:hover,
.el-menu--horizontal .el-menu-item.is-active {
  background: linear-gradient(90deg, #e8f5e9 60%, #fffde7 100%);
  color: #43a047 !important;
}

.nav-item i {
  margin-right: 4px;
  font-size: 16px;
}

.nav-item:hover i {
  color: #67c23a;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 18px;
}

.search-area {
  width: 240px;
  position: relative;
}

.search-input :deep(.el-input__inner) {
  border-radius: 24px;
  padding-left: 45px;
  height: 42px;
  line-height: 42px;
  font-size: 15px;
  border: 2px solid #a5d6a7;
  background: #f9fbe7;
  transition: all 0.3s ease;
}

.search-input :deep(.el-input__inner:hover) {
  background: #e8f5e9;
}

.search-input :deep(.el-input__inner:focus) {
  background: #fff;
  border-color: #67c23a;
  box-shadow: 0 0 0 2px rgba(103, 194, 58, 0.13);
}

.search-input :deep(.el-input__prefix) {
  left: 16px;
  line-height: 40px;
}

.search-input :deep(.el-input__icon) {
  font-size: 18px;
  color: #67c23a;
  transition: all 0.3s ease;
}

.search-input :deep(.el-input__inner:focus + .el-input__prefix .el-input__icon) {
  color: #43a047;
}

.search-input :deep(.el-input__suffix) {
  right: 12px;
}

.search-input :deep(.el-input__suffix-inner) {
  display: flex;
  align-items: center;
}

.search-input :deep(.el-input__clear) {
  color: #67c23a;
}

.search-input :deep(.el-input__clear:hover) {
  color: #43a047;
}

.user-area {
  display: flex;
  align-items: center;
  gap: 10px;
}

.login-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 18px;
  border-radius: 24px;
  background: linear-gradient(90deg, #e8f5e9 60%, #fffde7 100%);
  color: #43a047;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1.5px solid #a5d6a7;
}

.login-btn:hover {
  background: #e8f5e9;
  transform: translateX(4px);
  color: #388e3c;
}

.login-btn i {
  font-size: 16px;
  transition: transform 0.3s ease;
}

.login-btn:hover i {
  transform: translateX(2px);
}

.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 18px;
  border-radius: 24px;
  transition: all 0.3s ease;
  border: 1.5px solid #a5d6a7;
}

.user-dropdown:hover {
  background: #e8f5e9;
}

.username {
  margin-right: 8px;
  font-size: 15px;
  color: #388e3c;
}

.admin-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 18px;
  border-radius: 24px;
  background: linear-gradient(90deg, #e3f2fd 60%, #fffde7 100%);
  color: #409EFF;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1.5px solid #b3e5fc;
}

.admin-btn:hover {
  background: #e3f2fd;
  transform: translateX(4px);
  color: #1976d2;
}

.admin-btn i {
  font-size: 16px;
  transition: transform 0.3s ease;
}

.admin-btn:hover i {
  transform: translateX(2px);
}

@media (max-width: 1400px) {
  .header-container {
    padding: 0 10px;
  }
  .nav-area {
    margin: 0 10px;
  }
  .nav-menu {
    min-width: 600px;
  }
  .nav-item {
    padding: 0 10px;
  }
  .search-area {
    width: 180px;
  }
}

@media (max-width: 1100px) {
  .header-container {
    flex-direction: column;
    height: auto;
    padding: 0 4px;
    gap: 8px;
    align-items: stretch;
  }
  .nav-area {
    margin: 0;
    min-width: 0;
    width: 100%;
    justify-content: flex-start;
  }
  .nav-menu {
    min-width: 0;
    width: 100%;
    justify-content: flex-start;
  }
  .header-right {
    width: 100%;
    justify-content: flex-end;
    gap: 8px;
  }
  .search-area {
    width: 120px;
  }
}

@media (max-width: 700px) {
  .header-container {
    flex-direction: column;
    height: auto;
    padding: 0 2px;
    gap: 4px;
  }
  .logo-area {
    margin-bottom: 4px;
    padding: 0 4px;
    border-radius: 10px;
  }
  .logo-icon {
    width: 32px;
    height: 32px;
    margin-right: 6px;
    border-radius: 8px;
  }
  .logo-text {
    font-size: 15px;
  }
  .nav-area {
    margin: 0;
    min-width: 0;
    width: 100%;
    justify-content: flex-start;
  }
  .nav-menu {
    min-width: 0;
    width: 100%;
    justify-content: flex-start;
  }
  .nav-item {
    font-size: 12px;
    padding: 0 4px;
    height: 36px;
    line-height: 36px;
    border-radius: 6px 6px 0 0;
  }
  .header-right {
    width: 100%;
    justify-content: flex-end;
    gap: 4px;
  }
  .search-area {
    width: 80px;
  }
  .login-btn, .user-dropdown, .admin-btn {
    padding: 4px 8px;
    font-size: 12px;
    border-radius: 12px;
  }
}
</style> 